package gui;

import java.io.IOException;

import sistema.Sistema;

import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QVBoxLayout;

public class InviteWindow extends QDialog {

	private QLabel info;

	private QLineEdit email;

	private QPushButton ok;
	private QPushButton cancel;

	private Sistema sistema;
	private String userName;

	private QVBoxLayout mainLayout;
	private QHBoxLayout lay1;
	private QHBoxLayout lay2;

	public InviteWindow(Sistema sistema, String userName) {
		this.sistema = sistema;
		this.userName = userName;
		configureLayouts();
		createConnections();
		this.setLayout(mainLayout);
	}

	private void configureLayouts() {
		this.mainLayout = new QVBoxLayout();
		configureLayout1();
		configureLayout2();
		this.mainLayout.addLayout(lay1);
		this.mainLayout.addLayout(lay2);
	}

	private void configureLayout1() {
		this.lay1 = new QHBoxLayout();
		this.info = new QLabel("Informe o email do seu amigo:");
		this.email = new QLineEdit();
		this.lay1.addWidget(info);
		this.lay1.addWidget(email);
	}

	private void configureLayout2() {
		this.lay2 = new QHBoxLayout();
		this.ok = new QPushButton("OK");
		this.cancel = new QPushButton("Cancelar");
		this.lay2.addWidget(ok);
		this.lay2.addWidget(cancel);
	}

	@SuppressWarnings("unused")
	private void invite() {
		try {
			sistema.enviarConvite(userName, email.text());
			QMessageBox.information(this, "Sucesso!",
					"Convite enviado com sucesso para " + email.text());
		} catch (IOException e) {
			QMessageBox.critical(this, "Error", e.getMessage());
		}
		this.close();
	}

	private void createConnections() {
		this.ok.clicked.connect(this, "invite()");
		this.ok.clicked.connect(this, "close()");
	}

}